import 'package:flutter/material.dart';
import 'package:flutter_application_1/ui/page/business_page.dart';
import 'package:flutter_application_1/ui/page/home_page.dart';
import 'package:flutter_application_1/ui/page/my_page.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool darkModeOn = false;
  bool customTheme = true;
  int _selectedIndex = 0;
  final _pageController = PageController(initialPage: 0);

  // static const TextStyle optionStyle =
  //     TextStyle(fontSize: 30, fontWeight: FontWeight.bold);

  // static const List<Widget> _widgetOptions = <Widget>[
  //   Text(
  //     'Index 0: Home',
  //     style: optionStyle,
  //   ),
  //   Text(
  //     'Index 1: Business',
  //     style: optionStyle,
  //   ),
  //   Text(
  //     'Index 2: School',
  //     style: optionStyle,
  //   ),
  // ];

  @override
  void initState() {
    super.initState();
    // WidgetsBinding.instance.addObserver(this as WidgetsBindingObserver);
  }

  @override
  void dispose() {
    // WidgetsBinding.instance.removeObserver(this as WidgetsBindingObserver);
    super.dispose();
    _pageController.dispose();
  }

  void didChangePlatformBrightness() {
    Brightness brightness = WidgetsBinding.instance.window.platformBrightness;
    if (!customTheme) {
      setState(() {
        if (brightness.name == "light") {
          darkModeOn = false;
        } else {
          darkModeOn = true;
        }
      });
    }
  }

  // 底部导航栏跳转
  void _onItemTapped(int index) {
    _pageController.jumpToPage(index);
    setState(() {
      _selectedIndex = index;
    });
  }

  void changeBackThemeState(bool state) {
    setState(() {
      darkModeOn = state;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          fontFamily: 'AaShuYunSong',
          brightness: customTheme && !darkModeOn
              ? Brightness.light
              : customTheme && darkModeOn
                  ? Brightness.dark
                  : Brightness.light),
      darkTheme: ThemeData(
          fontFamily: 'AaShuYunSong',
          brightness: customTheme && !darkModeOn
              ? Brightness.light
              : customTheme && darkModeOn
                  ? Brightness.dark
                  : Brightness.dark),
      home: Scaffold(
        body: PageView(
            controller: _pageController,
            onPageChanged: (index) {
              setState(() {
                _selectedIndex = index;
              });
            },
            children: [
              const HomePage(),
              const BusinessPage(),
              MyPage(
                  darkState: darkModeOn, changeBackTheme: changeBackThemeState),
            ]),
        bottomNavigationBar: BottomNavigationBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(icon: Icon(Icons.home), label: '主页'),
            BottomNavigationBarItem(icon: Icon(Icons.business), label: '工作区'),
            BottomNavigationBarItem(icon: Icon(Icons.school), label: '我的')
          ],
          currentIndex: _selectedIndex,
          selectedItemColor: const Color.fromARGB(255, 14, 8, 1),
          onTap: _onItemTapped,
        ),
      ),
    );
  }
}
